package com.example.demo.selenium;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.JavascriptExecutor;

import java.time.Duration;
import java.util.List;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

public class WebCrawler {
    
    public WebDriver driver;
    public WebDriverWait wait;
    
    public WebCrawler() {
        try {
            // 设置控制台输出编码为 UTF-8
            System.setOut(new PrintStream(System.out, true, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        
        // 设置 Chrome 驱动,会自动根据浏览器版本下载对应的驱动
        WebDriverManager.chromedriver().setup();
        //System.setProperty("webdriver.chrome.driver", "D:\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
        
        // 配置 Chrome 选项
        ChromeOptions options = new ChromeOptions();
       // options.addArguments("--headless=new"); // 使用新的无头模式
        options.addArguments("--disable-gpu");
        options.addArguments("--no-sandbox");
        options.addArguments("--disable-dev-shm-usage");
        options.addArguments("--remote-allow-origins=*");
        options.addArguments("--disable-blink-features=AutomationControlled");
        options.addArguments("--disable-extensions");
        options.addArguments("--disable-infobars");
        options.addArguments("--start-maximized");
        options.addArguments("--disable-notifications");
        
        // 初始化 WebDriver
        driver = new ChromeDriver(options);
        // 设置隐式等待
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        // 最大化窗口
        driver.manage().window().maximize();
        
        // 初始化 WebDriverWait，用于显式等待
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }
    
    /**
     * 等待页面加载完成
     */
    public void waitForPageLoad() {
        // 等待页面状态为 complete
        wait.until(webDriver -> ((JavascriptExecutor) webDriver)
            .executeScript("return document.readyState").equals("complete"));
            
        // 等待 body 元素出现
        wait.until(ExpectedConditions.presenceOfElementLocated(By.tagName("body")));
    }
    
    /**
     * 爬取指定 URL 的页面内容并点击搜索按钮
     * @param url 目标网址
     */
    public void crawl(String url) {
        try {
            // 访问目标网页
            driver.get(url);

            // 获取页面标题
            String title = driver.getTitle();
            System.out.println("页面标题: " + title);
            // 查找并点击搜索按钮
            try {
            
                // 等待页面响应
                Thread.sleep(2000);
                
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            
            // 获取所有链接
            List<WebElement> links = driver.findElements(By.tagName("a"));
            System.out.println("页面链接数量: " + links.size());
            
            // 打印前10个链接
            for (int i = 0; i < Math.min(10, links.size()); i++) {
                WebElement link = links.get(i);
                System.out.println("链接 " + (i + 1) + ": " + link.getAttribute("href"));
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 关闭浏览器
     */
    public void close() {
        if (driver != null) {
            driver.quit();
        }
    }
    
    /**
     * 获取 WebDriver 实例
     */
    public WebDriver getDriver() {
        return driver;
    }
} 